//
//  ViewController.m
//  计算面积
//
//  Created by student on 16/2/20.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,weak) UIImageView *rectangleImageView;

@property (nonatomic,weak) UIImageView *circleImageView;

@property (nonatomic,weak) UIButton *calculateBtn;

@property (nonatomic,weak) UITextField *rectangleLength;

@property (nonatomic,weak) UITextField *rectangleWidth;

@property (nonatomic,weak) UITextField *rectangleArea;

@property (nonatomic,weak) UITextField *circleradius;

@property (nonatomic,weak) UITextField *circleArea;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setUpUI];
}

-(void)setUpUI{
    self.rectangleImageView = [self setUpImageViewWithFrame:CGRectMake(50, 50, 180, 180) WithImageName:@"RectArea"];
    self.circleImageView = [self setUpImageViewWithFrame:CGRectMake(50, 250, 180, 180) WithImageName:@"CircleArea"];
    [self setUpLabelWithFrame:CGRectMake(250, 50, 70, 50) WithText:@"length:"];
    [self setUpLabelWithFrame:CGRectMake(250, 120, 70, 50) WithText:@"Width:"];
    [self setUpLabelWithFrame:CGRectMake(250, 190, 70, 50) WithText:@"Area:"];
    [self setUpLabelWithFrame:CGRectMake(250, 260, 70, 50) WithText:@"Radius:"];
    [self setUpLabelWithFrame:CGRectMake(250, 330, 70, 50) WithText:@"Area:"];
    self.rectangleLength = [self setUpTextViewWithFrame:CGRectMake(330, 50, 70, 50)];
    self.rectangleWidth = [self setUpTextViewWithFrame:CGRectMake(330, 120, 70, 50)];
    self.rectangleArea = [self setUpTextViewWithFrame:CGRectMake(330, 190, 70, 50)];
    self.circleradius = [self setUpTextViewWithFrame:CGRectMake(330, 260, 70, 50)];
    self.circleArea = [self setUpTextViewWithFrame:CGRectMake(330, 330, 70, 50)];
    self.calculateBtn = [self setUpBtnWithFrame:CGRectMake(182, 450, 50, 50) Withtext:@"计算"];
    [self.calculateBtn addTarget:self action:@selector(calculateArea) forControlEvents:UIControlEventTouchUpInside];
}
-(UIImageView *)setUpImageViewWithFrame:(CGRect)frame
                 WithImageName:(NSString *)name{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    [imageView setImage:[UIImage imageNamed:name]];
    [self.view addSubview:imageView];
    return imageView;
}
-(void)setUpLabelWithFrame:(CGRect)frame
                  WithText:(NSString *)text{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:text];
    [label setTextColor:[UIColor blackColor]];
    [label setTextAlignment:NSTextAlignmentRight];
    [self.view addSubview:label];
}
-(UITextField *)setUpTextViewWithFrame:(CGRect)frame{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    [textField setTextAlignment:NSTextAlignmentCenter];
    [textField setTextColor:[UIColor redColor]];
    [self.view addSubview:textField];
    textField.hidden = NO;
    return textField;
}
-(UIButton *)setUpBtnWithFrame:(CGRect)frame
                      Withtext:(NSString *)text{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    return btn;
}
-(void)calculateArea{
    float rectangleLength = [self.rectangleLength.text floatValue];
    float rectangleWidth = [self.rectangleWidth.text floatValue];
    [self.rectangleArea setText:[NSString stringWithFormat:@"%.2f",rectangleLength*rectangleWidth]];
    float radius = [self.circleradius.text floatValue];
    [self.circleArea setText:[NSString stringWithFormat:@"%.2f",radius*radius*M_PI]];
    
}

- (IBAction)hideKeyBoard:(id)sender {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
